package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("file")
    public BaseRespVo fileUpload(MultipartFile myfile) throws IOException {
        //生成一些随机数作为文件名 👉 避免覆盖
        //原始的文件名
        String originalFilename = myfile.getOriginalFilename();
        //请求参数名 myfile
        String name = myfile.getName();
        //获得正文类型 Content-Type → 文件类型
        String contentType = myfile.getContentType();
        //文件大小
        long size = myfile.getSize();

        File file = new File("D:\\spring", originalFilename);
        //给transferTo方法提供一个file → 指定接收的file 👉 接收的路径和文件名
        myfile.transferTo(file);
        return BaseRespVo.ok();
    }
    @RequestMapping("files")
    public BaseRespVo filesUpload(MultipartFile[] myfiles) throws IOException {

        for (MultipartFile myfile : myfiles) {
            String originalFilename = myfile.getOriginalFilename();
            File file = new File("D:\\spring", originalFilename);
            //给transferTo方法提供一个file → 指定接收的file 👉 接收的路径和文件名
            myfile.transferTo(file);
        }

        return BaseRespVo.ok();
    }
}
